package cn.mayaslight.wechat.api;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;

import cn.mayaslight.common.handler.LocalHttpClient;
import cn.mayaslight.common.utils.JsonUtil;
import cn.mayaslight.wechat.api.bean.BaseResult;
import cn.mayaslight.wechat.api.bean.FollowResult;
import cn.mayaslight.wechat.api.bean.Group;
import cn.mayaslight.wechat.api.bean.Openid;
import cn.mayaslight.wechat.api.bean.User;
import cn.mayaslight.wechat.api.bean.UserInfo;
import cn.mayaslight.wechat.api.bean.Userid;

public class UserAPI extends BaseAPI {
	
	/**
	 * @param access_token
	 * @param openid
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static Userid qyGetUserid(String access_token, String openid) throws Exception {
		RequestBuilder builder = RequestBuilder.post().setUri(QY_BASE_URI + "/cgi-bin/user/convert_to_userid?access_token="+access_token);
		Map<String, String> map = new HashMap<String, String>();
		map.put("openid", openid);
		HttpUriRequest httpUriRequest = builder.setEntity(new StringEntity(JsonUtil.toJSONString(map))).build();
		return (Userid) LocalHttpClient.executeJsonResult(httpUriRequest,Userid.class);
	}

	/**
	 * @param access_token
	 * @param userid
	 * @param agentid
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static Openid qyGetOpenid(String access_token, String userid, String agentid) throws Exception {
		RequestBuilder builder = RequestBuilder.post().setUri(QY_BASE_URI + "/cgi-bin/user/convert_to_openid?access_token="+access_token);
		Map<String, String> map = new HashMap<String, String>();
		map.put("userid", userid);
		map.put("agentid", agentid);
		HttpUriRequest httpUriRequest = builder.setEntity(new StringEntity(JsonUtil.toJSONString(map))).build();
		return (Openid) LocalHttpClient.executeJsonResult(httpUriRequest,Openid.class);
	}

	/**
	 * @param access_token
	 * @param code
	 * @return
	 */
	public static UserInfo qyUserInfo(String access_token, String code) {
		HttpUriRequest httpUriRequest = RequestBuilder.get().setUri(QY_BASE_URI + "/cgi-bin/user/getuserinfo").addParameter("access_token", access_token).addParameter("code", code).build();
		return (UserInfo) LocalHttpClient.executeJsonResult(httpUriRequest, UserInfo.class);
	}

	/**
	 * @param access_token
	 * @param openid
	 * @return
	 */
	public static User userInfo(String access_token, String openid) {
		HttpUriRequest httpUriRequest = RequestBuilder.post().setUri(BASE_URI + "/cgi-bin/user/info").addParameter("access_token", access_token).addParameter("openid", openid).addParameter("lang", "zh_CN").build();
		return (User) LocalHttpClient.executeJsonResult(httpUriRequest, User.class);
	}

	/**
	 * @param access_token
	 * @param next_openid
	 * @return
	 */
	public static FollowResult userGet(String access_token, String next_openid) {
		HttpUriRequest httpUriRequest = RequestBuilder.post().setUri(BASE_URI + "/cgi-bin/user/get").addParameter("access_token", access_token).addParameter("next_openid", next_openid == null ? "" : next_openid).build();
		return (FollowResult) LocalHttpClient.executeJsonResult(httpUriRequest, FollowResult.class);
	}

	/**
	 * @param access_token
	 * @param name
	 * @return
	 */
	public static Group groupsCreate(String access_token, String name) {
		String groupJson = "{\"group\":{\"name\":\"" + name + "\"}}";
		HttpUriRequest httpUriRequest = RequestBuilder.post().setHeader(jsonHeader).setUri(BASE_URI + "/cgi-bin/groups/create").addParameter("access_token", access_token).setEntity(new StringEntity(groupJson, Charset.forName("utf-8"))).build();
		return (Group) LocalHttpClient.executeJsonResult(httpUriRequest, Group.class);
	}

	/**
	 * @param access_token
	 * @return
	 */
	public static Group groupsGet(String access_token) {
		HttpUriRequest httpUriRequest = RequestBuilder.post().setUri(BASE_URI + "/cgi-bin/groups/get").addParameter("access_token", access_token).build();
		return (Group) LocalHttpClient.executeJsonResult(httpUriRequest, Group.class);
	}

	/**
	 * @param access_token
	 * @param openid
	 * @return
	 */
	public static Group groupsGetid(String access_token, String openid) {
		String groupJson = "{\"openid\":\"" + openid + "\"}";
		HttpUriRequest httpUriRequest = RequestBuilder.post().setHeader(jsonHeader).setUri(BASE_URI + "/cgi-bin/groups/getid").addParameter("access_token", access_token).setEntity(new StringEntity(groupJson, Charset.forName("utf-8"))).build();
		return (Group) LocalHttpClient.executeJsonResult(httpUriRequest, Group.class);
	}

	/**
	 * @param access_token
	 * @param id
	 * @param name
	 * @return
	 */
	public static BaseResult groupsUpdate(String access_token, String id, String name) {
		String groupJson = "{\"group\":{\"id\":" + id + ",\"name\":\"" + name + "\"}}";
		HttpUriRequest httpUriRequest = RequestBuilder.post().setHeader(jsonHeader).setUri(BASE_URI + "/cgi-bin/groups/update").addParameter("access_token", access_token).setEntity(new StringEntity(groupJson, Charset.forName("utf-8"))).build();
		return (BaseResult) LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}

	/**
	 * @param access_token
	 * @param openid
	 * @param to_groupid
	 * @return
	 */
	public static BaseResult groupsMembersUpdate(String access_token, String openid, String to_groupid) {
		String groupJson = "{\"openid\":\"" + openid + "\",\"to_groupid\":" + to_groupid + "}";
		HttpUriRequest httpUriRequest = RequestBuilder.post().setHeader(jsonHeader).setUri(BASE_URI + "/cgi-bin/groups/menbers/update").addParameter("access_token", access_token).setEntity(new StringEntity(groupJson, Charset.forName("utf-8"))).build();
		return (BaseResult) LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}

}
